//
//  FirstView.m
//  SweetComics
//
//  Created by LeeZhe on 15/1/29.
//  Copyright (c) 2015年 LeeZhe. All rights reserved.
//

#import "FirstView.h"

@implementation FirstView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)createView
{
    
    
    //    UICollectionViewFlowLayout * flowLayOut = [[UICollectionViewFlowLayout alloc] init];
    // 每个项的大小
    //    flowLayOut.itemSize = CGSizeMake(90,400);
    
    float width = self.frame.size.width;
    float height = self.frame.size.height;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, width, height)
                                             collectionViewLayout:[[CircleLayout alloc] init]];
    [self.collectionView registerClass:[ShowImageCell class] forCellWithReuseIdentifier:KCellIdentifier];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    //    self.collectionView.pagingEnabled = YES;
    [self.collectionView setContentOffset:CGPointMake(width, 0.0F)];
    self.collectionView.dataSource = self;
    
    [self conectNetWork];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100, 50)];
    label.text = @"热门漫画";
    label.textColor = [UIColor orangeColor];
    label.font = [UIFont systemFontOfSize:20];
    self.backView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, self.frame.size.width, self.frame.size.height)];
    _backView.image = [UIImage imageNamed:@"背景.jpg"];
    _collectionView.backgroundView = _backView;
//    self.rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    [_rightButton setTitle:@"浏览全部" forState:UIControlStateNormal];
//    _rightButton.frame = CGRectMake(self.frame.size.width - 100,15,100,40);
//    _rightButton.titleLabel.font = [UIFont systemFontOfSize:16];
    
    [self addSubview:_collectionView];
//    [self addSubview:_rightButton];
    [self addSubview:label];
    [label release];
    
}
- (void)conectNetWork
{
    self.arr = [NSMutableArray array];
    __block  FirstView *homePage = self;
    [ConnectRequest getConnect:kHotURL block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *comicArr = [dic objectForKey:@"data"];
        for (NSMutableDictionary *comicDic in comicArr) {
            CommicsList *commics = [[CommicsList alloc] init];
            [commics setValuesForKeysWithDictionary:comicDic];
            [homePage.arr addObject:commics];
            [homePage.collectionView reloadData];
        }
    }];
    
}




- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   
    return self.arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    // must be dequeueReusableCellWithReuseIdentifier !!!!
    ShowImageCell *cell = (ShowImageCell *)[cView dequeueReusableCellWithReuseIdentifier:KCellIdentifier
                                                                            forIndexPath:indexPath];
    if (!cell) {
        NSLog(@"what ? cell is nil ? It's joke !");
        return nil;
    }
    CommicsList *comics = [self.arr objectAtIndex:indexPath.row];
    [ConnectRequest getConnect:comics.cover_url block:^(NSData *data) {
        cell.imageView.image = [UIImage imageWithData:data];
    }];
    
    [cell setComicsCell:comics];
    
    return cell;
    
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
    //无限循环....
    float targetX = _scrollView.contentOffset.x;
    NSInteger numCount = [self.collectionView numberOfItemsInSection:0];
    float ITEM_WIDTH = _scrollView.frame.size.width;
    
    if (numCount>=3)
    {
        if (targetX < ITEM_WIDTH/2) {
            [_scrollView setContentOffset:CGPointMake(targetX+ITEM_WIDTH *numCount, 0)];
        }
        else if (targetX >ITEM_WIDTH/2+ITEM_WIDTH *numCount)
        {
            [_scrollView setContentOffset:CGPointMake(targetX-ITEM_WIDTH *numCount, 0)];
        }
    }
    
}
- (void)dealloc
{
    [_backView release];
    [_rightButton release];
    [_cell release];
    [_collectionView release];
    [super dealloc];
}


@end
